Methods to retrieve the data from the result set class
Sql type |
Java type |
Methods to Retrieve |
Char,varchar |
String |
getString() |
Numeric |
Java.math.bigdecimal |
getBigdecimal() |
Bit |
Boolean |
getBoolean() |
Integer |
Integer |
getInt() |
Real |
Float |
getFloat() |
Date |
Java.sql.date |
getDate() |
Time |
Java.sql.time |
getTime() |
Write a Program to read the data from a Table.
import java.sql.*;
public class Insert
{
public static void main(String args[])
{
Connection con=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:vision","scott","tiger");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(“select * from abc”);
while(rs.next())
{
System.out.println(rs.getInt(1)+” “+rs.getString(2)+” “+
rs.getInt(3));
// System.out.println(rs.getInt(“rno”)+” “+rs.getString(“na”)
+” “+rs.getInt(“sal”));
}
rs.close();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(con!=null)
con.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
}